﻿using DotNetMVC.Common;
using DotNetMVC.DTO;
using DotNetMVC.IService;
using DotNetMVC.Website.Areas.WebAdmin.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DotNetMVC.Website.Areas.WebAdmin.Controllers
{
    public class FunctionalModuleController : Controller
    {
        public IFunctionalModuleService functionalModuleService { get; set; }
        // GET: WebAdmin/FunctionalModule
        public ActionResult List()
        {
            FunctionalModuleDTO[] model = functionalModuleService.GetFunctionalModules();
            return View(model);
        }

        [HttpGet]
        public string ExistByFunCode(string funCode)
        {
            //remote:远程地址只能输出 "true" 或 "false"，不能有其他输出。
            //存在： "false"
            return functionalModuleService.ExistByFunCode(funCode) ? "false" : "true";
        }

        [HttpGet]
        public ActionResult Add()
        {
            #region 是否启用
            List<SelectListItem> listIsEnabled = new List<SelectListItem>();
            listIsEnabled.Add(new SelectListItem { Value = "1", Text = "是" });
            listIsEnabled.Add(new SelectListItem { Value = "0", Text = "否" });
            ViewBag.ListIsEnabled = listIsEnabled;
            #endregion

            return View();
        }

        [HttpPost]
        public ActionResult Add(string funName, string funCode, string ctrlUrl, string viewHtmlTemplate, string listHtmlTemplate, int sort, int isEnabled, string remark)
        {
            if (string.IsNullOrEmpty(funName))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "名称不能为空" });
            }
            if (string.IsNullOrEmpty(funCode))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "代码不能为空" });
            }
            if (functionalModuleService.ExistByFunCode(funCode))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "代码已存在" });
            }
            functionalModuleService.Add(funName, funCode, ctrlUrl, viewHtmlTemplate, listHtmlTemplate, sort, isEnabled == 1, remark);

            //操作日志
            AdminHelper.AddUserLog(HttpContext, "新增功能分类：" + funName);

            return Json(new AjaxResult { Status = "ok" });
            //return View();
        }

        [HttpGet]
        public ActionResult Edit(long id)
        {
            FunctionalModuleDTO model = functionalModuleService.GetDTOById(id);
            if (model == null)
            {
                return View("Error", (object)"获取不到功能分类信息");
            }

            #region 是否启用
            List<SelectListItem> listIsEnabled = new List<SelectListItem>();
            listIsEnabled.Add(new SelectListItem { Value = "1", Text = "是" });
            listIsEnabled.Add(new SelectListItem { Value = "0", Text = "否" });
            ViewBag.ListIsEnabled = new SelectList(listIsEnabled, "Value", "Text", model.IsEnabled ? "1":"0");
            #endregion

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(long id, string funName, string funCode, string ctrlUrl, string viewHtmlTemplate, string listHtmlTemplate, int sort, int ddlIsEnabled, string remark)
        {
            if (string.IsNullOrEmpty(funName))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "名称不能为空" });
            }
            if (string.IsNullOrEmpty(funCode))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "功能分类代码不能为空" });
            }
            FunctionalModuleDTO model = functionalModuleService.GetDTOById(id);
            if (model == null)
            {
                return View("Error", (object)"获取不到功能分类信息");
            }
            if (model.FunCode != funCode && functionalModuleService.ExistByFunCode(funCode))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "功能分类代码已存在，请更换" });
            }
            functionalModuleService.Update(id, funName, funCode, ctrlUrl, viewHtmlTemplate, listHtmlTemplate, sort, ddlIsEnabled == 1, remark);

            //操作日志
            AdminHelper.AddUserLog(HttpContext, "修改功能分类:" + funName + "（Id:" + id + "）");

            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        public ActionResult Delete(long id)
        {
            FunctionalModuleDTO model = functionalModuleService.GetDTOById(id);
            if (model == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "获取不到功能分类信息" });
            }
            functionalModuleService.MarkDeleted(id);

            //操作日志
            AdminHelper.AddUserLog(HttpContext, "删除功能分类" + model.FunName + "（Id:" + id + "）");
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        public ActionResult BatchDelete(long[] selectdIds)
        {
            if (selectdIds.Length <= 0)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未选中删除数据" });
            }

            List<long> list = new List<long>();
            foreach (long id in selectdIds)
            {
                list.Add(id);
            }
            functionalModuleService.BatchMarkDeleted(list);

            //操作日志
            AdminHelper.AddUserLog(HttpContext, "批量删除功能分类（Ids:" + string.Join(",", list) + "）");

            return Json(new AjaxResult { Status = "ok" });
        }
    }
}